<?php

class DBService {
	
	private static $dbConfig = array (
			"user0"=>"localhost:10101",
			"user1"=>"localhost:10101",
			"user2"=>"localhost:10101",
			"user3"=>"localhost:10101",
			"user4"=>"localhost:10101",
			"user5"=>"localhost:10101",
			"user6"=>"localhost:10101",
			"user7"=>"localhost:10101",
			"user8"=>"localhost:10101",
			"user9"=>"localhost:10101",
			
			"note0"=>"localhost:10101",
			"note1"=>"localhost:10101",
			"note2"=>"localhost:10101",
			"note3"=>"localhost:10101",
			"note4"=>"localhost:10101",
			"note5"=>"localhost:10101",
			"note6"=>"localhost:10101",
			"note7"=>"localhost:10101",
			"note8"=>"localhost:10101",
			"note9"=>"localhost:10101",
			
			"global"=>"localhost:10101"
			);
	
	private static $connCache = array();
	
	public static function getGlobalDb() {
		try {
			$dbName = "global";
			$dbStr = DBService::$dbConfig[$dbName];
			if(!array_key_exists($dbName, DBService::$connCache)) {
				$conn = new Mongo($dbStr);
				DBService::$connCache[$dbName]=$conn;
			}
			return DBService::$connCache[$dbName]->$dbName;
		} catch(Exception $e) {
			return null;
		}
	}
	
	public static function getDbById($name, $id) {
		try {
			$dbIndex = intval(($id / 10) % 10);
			$dbName = $name.$dbIndex;
			$dbStr = DBService::$dbConfig[$dbName];
			if(!array_key_exists($dbName, DBService::$connCache)) {
				$conn = new Mongo($dbStr);
				DBService::$connCache[$dbName]=$conn;
			}
			return DBService::$connCache[$dbName]->$dbName;
		} catch(Exception $e) {
			return null;
		}
	}
	
	public static function getCollectionNameById($name, $id) {
		return $name.intval($id % 10);
	}
}
